﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ShenPiControl1 : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            Loaded();
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        public ObservableCollection<RenQianKaoCha> models = new ObservableCollection<RenQianKaoCha>();
        public int OrgID = 0;
        private string OrgName = "";
        public ShenPiControl1(int orgID,string orgName)
        {
            InitializeComponent();
            OrgID = orgID;
            OrgName = orgName;
            if (Session.LoginUser.LoginName == "admin")
            {
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
                scrol.IsEnabled = false;
            }
            else
            {
                OKButton.Visibility = System.Windows.Visibility.Visible;
                next.IsEnabled = false;
                scrol.IsEnabled = true;
            }
            Loaded();
        }
        public void Loaded()
        {
            Client.GetKaoChaQingKuangCompleted -= new EventHandler<GetKaoChaQingKuangCompletedEventArgs>(Client_GetKaoChaQingKuangCompleted);
            Client.GetKaoChaQingKuangCompleted += new EventHandler<GetKaoChaQingKuangCompletedEventArgs>(Client_GetKaoChaQingKuangCompleted);
            Client.GetKaoChaQingKuangAsync(OrgID);
        }

        void Client_GetKaoChaQingKuangCompleted(object sender, GetKaoChaQingKuangCompletedEventArgs e)
        {
            stackpanel.Children.Clear();
            if (e.Result != null && e.Result.Count > 0)
            {
                OKButton.IsEnabled = true;
                if(e.Result[0].ID != 0) next.IsEnabled = true;
                models = e.Result;
                int count = ((ObservableCollection<RenQianKaoCha>)e.Result).Count;
                for (int i = 0; i < count; i++)
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = e.Result[i].EmpName;
                    tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    tb.Margin = new Thickness(3, 3, 3, 3);
                    TextBox t = new TextBox();
                    t.Height = 120;
                    t.Width = 600;
                    t.Margin = new Thickness(3, 3, 3, 3);
                    StackPanel sp = new StackPanel();
                    sp.Orientation = Orientation.Horizontal;
                    sp.Margin = new Thickness(3, 3, 3, 3);
                    sp.Children.Add(tb);
                    sp.Children.Add(t);

                    Binding binding = new Binding();
                    binding.Source = e.Result[i];
                    binding.Path = new PropertyPath("Memo");
                    binding.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t, TextBox.TextProperty, binding);
                    stackpanel.Children.Add(sp);
                }
            }
            else OKButton.IsEnabled = false;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            ShenPiControl2 view = new ShenPiControl2(Convert.ToInt32(OrgID), OrgName);
            view.Nav = Nav;
            Nav.ShowNextPage(OrgName + "-党（工）委（组）讨论情况", view, this);
        }

        private int c = 0;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定上报考察情况吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (RenQianKaoCha i in models)
                {
                    Client.InsertRenQianKaoChaCompleted -=
                    new EventHandler<InsertRenQianKaoChaCompletedEventArgs>(Client_InsertRenQianKaoChaCompleted);
                    Client.InsertRenQianKaoChaCompleted +=
                        new EventHandler<InsertRenQianKaoChaCompletedEventArgs>(Client_InsertRenQianKaoChaCompleted);
                    Client.InsertRenQianKaoChaAsync(i);
                    c = c + 1;
                }
                
            }

        }

        void Client_InsertRenQianKaoChaCompleted(object sender, InsertRenQianKaoChaCompletedEventArgs e)
        {
            if(c == models.Count)
            {
                c = c - 1;
                ShenPiControl2 view = new ShenPiControl2(Convert.ToInt32(OrgID),OrgName);
                view.Nav = Nav;
                Nav.ShowNextPage(OrgName + "-党（工）委（组）讨论情况", view, this);
            }
        }
    }
}
